import 'package:flutter/material.dart';
import 'dart:math' as math;
import 'dart:ui';

class WaterMark extends StatefulWidget {
  @override
  _WaterMarkState createState() => _WaterMarkState();
}

class _WaterMarkState extends State<WaterMark>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;
  static const String watermarktext = '密密密密';

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this);
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('加水印')),
      backgroundColor: Colors.pink[100],
      body: ListView(
        children: [
          // Watermark1(
          //   child: Container(
          //     width: 60,
          //     height: 300,
          //     color: Colors.yellowAccent,
          //     child: FlutterLogo(),
          //   ),
          //   watermark: watermarktext,
          // ),
          SizedBox(
            height: 280,
          ),
          Center(
            child: Container(
              child: _buildItem(),
              width: 300,
              height: 500,
              color: Colors.yellow,
            ),
          ),
          SizedBox(
            height: 280,
          ),
          // SizedBox(
          //   height: 280,
          //   child: _buildItem(),
          // ),
          // SizedBox(
          //   height: 300,
          //   child: Row(
          //     children: [
          //       Expanded(child: _buildItem()),
          //       Expanded(child: _buildItem())
          //     ],
          //   ),
          // ),
          // SizedBox(
          //   height: 280,
          //   child: _buildItem(),
          // ),
          // Center(
          //   child: SizedBox(
          //     height: 300,
          //     width: 200,
          //     child: _buildItem(),
          //   ),
          // ),
          // SizedBox(
          //   height: 400,
          //   child: _buildItem(),
          // ),
        ],
      ),
    );
  }

  Widget _buildItem() {
    return Container(
      margin: const EdgeInsets.all(3),
      // height: 280,
      child: WaterMarkTest(
        watermark: watermarktext,
        child: Center(
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [FlutterLogo(size: 10), Text('flutter')],
          ),
        ),
      ),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(5),
      ),
    );
  }
}

class WaterMarkTest extends StatelessWidget {
  @required
  final String watermark;
  @required
  final Widget child;

  const WaterMarkTest({Key key, this.watermark, this.child}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        child,
        Positioned.fill(
          child: LayoutBuilder(builder: (context, constraints) {
            return Transform.rotate(
                // angle: -math.pi / 4,
                angle: -45,
                child: FittedBox(
                    child: Text(
                  watermark,
                )));
          }),
        )
      ],
    );
  }
}

class Watermark1 extends StatelessWidget {
  @required
  final Widget child;
  final String watermark;

  final double scale = 0.707;

  const Watermark1({
    this.child,
    this.watermark,
  });

  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: Alignment.center,
      children: [
        child,
        if (watermark != null)
          ImageFiltered(
            imageFilter: ImageFilter.blur(sigmaX: 5, sigmaY: 5),
            child: child,
          ),
        if (watermark != null)
          Positioned.fill(
            //本来用了CustomSingleChildLayout，想了想没必要
            child: Container(
              alignment: Alignment.center,
              transform: Matrix4.rotationZ(-0.78)..scale(scale, scale, 1),
              transformAlignment: Alignment.center,
              child: AspectRatio(
                aspectRatio: 1,
                child: FittedBox(
                  child: Text(
                    watermark,
                    textScaleFactor: 100,
                  ),
                ),
              ),
            ),
          ),
      ],
    );
  }
}
